package io.renren.modules.home.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.modules.home.service.HomeService;
import io.renren.modules.shippinginformation.dao.ShippingInformationDao;
import io.renren.modules.shippinginformation.entity.ShippingInformationEntity;
import io.renren.modules.waybillorder.dao.WaybillOrderDao;
import io.renren.modules.waybillorder.entity.WaybillOrderEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author cyx
 * @date 2022/2/28-18:14
 */
@Service("homeServiceImpl")
public class HomeServiceImpl implements HomeService {
    @Autowired
    private WaybillOrderDao waybillOrderDao;
    @Autowired
    private ShippingInformationDao shippingInformationDao;
    @Override
    public Map<String, Object> getStatistics() {
        DateTime startTime = DateUtil.lastMonth();
        DateTime endTime = DateUtil.date();
        List<String> day = new ArrayList<>();
        List<Integer> count1 = new ArrayList<>();
        List<Integer> count2 = new ArrayList<>();
        do{
            QueryWrapper<WaybillOrderEntity> waybillOrderEntityQueryWrapper = new QueryWrapper<>();
            waybillOrderEntityQueryWrapper.lambda().like(WaybillOrderEntity::getCreatetime,DateUtil.format(startTime, DatePattern.NORM_DATE_FORMAT));
            Integer integer = waybillOrderDao.selectCount(waybillOrderEntityQueryWrapper);
            count1.add(integer);
            QueryWrapper<ShippingInformationEntity> shippingInformationEntityQueryWrapper = new QueryWrapper<>();
            shippingInformationEntityQueryWrapper.lambda().like(ShippingInformationEntity::getCreatetime,DateUtil.format(startTime, DatePattern.NORM_DATE_FORMAT));
            Integer integer2 = shippingInformationDao.selectCount(shippingInformationEntityQueryWrapper);
            count2.add(integer2);
            day.add(DateUtil.format(startTime, DatePattern.NORM_DATE_FORMAT));
            startTime = DateUtil.offsetDay(startTime, 1);
        }while (DateUtil.between(startTime, endTime, DateUnit.DAY)>0);
        Map<String,Object> map = new HashMap<>();
        map.put("day",day);
        map.put("count1",count1);
        map.put("count2",count2);
        return map;
    }
}
